package mvc;

import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.Shape;

import javax.swing.JFrame;
import javax.swing.JPanel;

import engine.Triangle;

/**
 * 
 * View component of the MVC system.
 * This handles the entire graphical front-end.
 * The main window should ALWAYS be resizable, so keep this in mind
 * whenever writing code for this class.
 * 
 * @author Dylan Swiggett
 * 
 */
public class View extends Thread {
	Model model;
	JFrame frame;
	JPanel panel;

	//If true and available, full screen mode will be used.
	private static final boolean FULLSCREENENABLED = false;
	private static final int padding = 100;	//Amount of padding on a default window.

	@SuppressWarnings("unused")	//Stop Eclipse's whining when FULLSCREENENABLED is false.
	public View(Model model) {
		this.model = model;	//Has one way access to the Model3
		
		frame = new JFrame("APOPS development version");
		panel = new JPanel();
		
		GraphicsEnvironment environment = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice device = environment.getScreenDevices()[0];

		/*
		 * TODO: Do we want to use a layout manager?
		 */
		Container contentPane = frame.getContentPane();
        contentPane.setLayout(null);
		
		if (device.isFullScreenSupported() && FULLSCREENENABLED) {
			/*
			 * Enable full screen mode, if supported.
			 */
			frame.setUndecorated(true);
			frame.setResizable(false);
//			device.getDisplayMode().	// TODO: Figure out how to remove bar from bottom...
			device.setFullScreenWindow(frame);
			frame.validate();
		} else {
			/*
			 * If full screen mode is not supported, instead enter windowed
			 * mode, with the window leaving padding on each edge of the screen.
			 */
			frame.setSize(device.getDisplayMode().getWidth() - padding * 2, device
					.getDisplayMode().getHeight() - padding * 2);
			frame.setLocation(padding, padding);
			frame.validate();
			frame.setVisible(true);
		}
		
		frame.add(panel);
		panel.setBounds(100, 100, 300, 300);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				System.err.println("Error: sleep interrupted in View");
				e.printStackTrace();
			}
			
			Graphics2D g = (Graphics2D) panel.getGraphics();
			
			for (Triangle t : model.polygon.triangles){
				t.draw(g);
			}
			
			//panel.repaint();
			//panel.update();
			
			continue;
		}
	}
}
